function contentPrepare($)
{
// удалить комментарий
  var removeComment = function($comment)
  {
    $.post([$.baseUrl, "/comments/remove"].join(""), {id: $comment.find(".comment:first").data("rec-id")}, function(show)
    {
      var $count = $("#comments label");
      var count  = $comment.find(".comment").length;
      if (show)
        count = -count;
      $count.text(parseInt($count.text()) - count);
      var $editor = $("#comment");
      if ($editor.length)
        $editor.empty().append("<textarea />").insertAfter("#add-comment");
      $comment.toggle(show).next(".sentenced").toggle(!show);
    });
    return false;
  };

  var initLinks = function($comment)
  {
    $comment.
      find(".add-comment").click(function()
      {
        var $this   = $(this);
        var $editor = $("#comment");
        var $old    = $editor.prev();
        var $new    = $this.parents("#add-comment,.comment").filter("[data-table][data-rec-id]:first");
        var hidden  = $editor.is(":hidden");
        $editor.slideUp("fast", function()
        {
          var $add = $old.find(".ui-icon-close").
                          removeClass("ui-icon-close").
                          addClass("ui-icon-comment").
                          end().
                          find(".add-comment[data-save]");
          $add.text($add.attr("data-save") || "").removeAttr("data-save");
          if (hidden || ($old[0] != $new[0]))
          {
            $this.attr("data-save", $this.text()).text("Отменить");
            $new.find(".ui-icon-comment").removeClass("ui-icon-comment").addClass("ui-icon-close");
            if ($old[0] != $new[0])
              $editor.empty().append("<textarea />").insertAfter($new);
            var $area = $editor.find("textarea");
            if ($area.length && !$area[0].elrte)
            {
              $area.elrte({height: 100, toolbar: "compact"});
              $editor.find(".panel-save .save").unbind("click").click(function()
              {
                $.ajax({
                  url     : [$.baseUrl, "/comments/add"].join(""),
                  data    : {comment: {comment: $area.elrte("val"), table: $new.data("table"), rec_id: $new.data("rec-id")}},
                  type    : "post",
                  dataType: "html",
                  success : function(comment)
                            {
                              $this.click();
                              var $count = $("#comments label");
                              $count.text(parseInt($count.text()) + 1);
                              var $list = $new.nextAll(".comments");
                              if (!$list.length)
                                $list = $("<div />", {"class": "comments"}).appendTo($new.parent());
                              var $comment = $(comment).appendTo($list);
                              initLinks($comment);
                              $comment.find("a:first").focus();
                            },
                  error   : function(){$area.fs().rteFocus();}
                });
              });
            }
            $editor.slideDown("fast", function(){$area.fs().rteFocus().elrte.updateHeight();});
          }
        });
        return false;
      }).end().
      find("a.del-comment").click(function(){return removeComment($(this).parents(":has(.comment):first"));}).end().
      find("a.restore-comment").click(function(){return removeComment($(this).parent().prev());}).end().
      find(".aprofile").jtip({urlAttr: "profile"});
  };

  $("#content form.update-post").fs().createFields(function(post)
  {
    if (post)
    {
      $("#no-image").uploader("clear");
      $("#content").
        find(".new-image").
        removeClass("new-image").
        end().
        find(".update-post").
        nextAll("div:first").
        find("h2").
        html(post.title).
        nextAll().
        filter(".text").
        html(post.content).
        end().
        filter(".tags").
        fs().addTag(post.tags);
      $("#illustrations").
        data("main-img", post.mainImage).
        html($.map(post.illustrations, function(photo)
        {
          return ["<div class='ui-state-default illustration'>",
                   "<a href='", $.baseUrl, "/content/img/illustrations/", photo, "' title='", post.title, "' rel='photos", post.id, "'>",
                     "<img src='", $.baseUrl, "/content/thumb/illustrations/", photo, "?s=96' />",
                   "</a>",
                 "</div>"].join("");
        }).join("&nbsp;")).
        find(".illustration a").
        fancybox({titlePosition: "over"});
      $("#update-post").click();
    }
  });

  $("#update-post").click(function()
  {
    var $this = $(this);
    var $form = $this.parent().next();
    if ($form.is(":hidden"))
    {
      var $post = $form.nextAll("div:first").
                        find(".tag[data-tag-id]").
                        each(function(){with($(this))$("#post-tags").fs().addTag(text(),data("tag-id"));}).
                        end().
                        hide();
      var $photos = $("#illustrations");
      var mainImg = $photos.data("main-img");
      $("#main-image-num").val(mainImg);
      $photos.find(".illustration").clone().each(function(i)
      {
        var $this = $(this);
        var $a    = $this.children("a");
        var img   = $a.children("img").attr("src").replace(/\?.*$/, "");
        $a.replaceWith(["<div style='background-image:url(", img, "?s=128);' />",
                        "<span class='ui-icon ui-icon-closethick' title='Удалить фото' />",
                        "<input type='hidden' name='illustrations[]' value='", img, "' />"].join(""));
        if (i == mainImg)
          $this.attr("id", "main-image");
      }).insertBefore($("#no-image").parent());
      $form.find("textarea").
            elrte("val", $post.find(".text").html()).
            end().
            show();
      $("#post-title").val($post.find("h2").html()).focus();
      $this.fs().changeLink();
    }
    else
    {
      $fs.clearPhotos();
      $("#post-tags").val("").removeAttr("title");
      $form.hide().
            nextAll("div:first").
            show().
            end().
            find(".tag").
            remove();
      $this.fs().changeLink("ui-icon-close", "ui-icon-pencil", "Править");
    }
    return false;
  });

  $("#content").
    find("a.del-post").click(function()
    {
      $fs.confirmation("Восстановить пост будет невозможно. Нажмите OK для продолжения", function()
      {
        $.post([$.baseUrl, "/posts/remove"].join(""), {id: $(this).parents(".post:first").data("post-id")}, function(response)
        {
          $fs.getContent(response.href.stripTags());
        });
      });
      return false;
    }).end().
    find(".vote[data-table][data-rec-id] a[data-score]").click(function()
    {
      var $this = $(this);
      var $vote = $this.parent();
      $.post(
        [$.baseUrl, "/rating/vote"].join(""),
        {key: {table: $vote.data("table"), rec_id: $vote.data("rec-id")}, score: $this.data("score")},
        function(){$vote.remove();}
      );
      return false;
    });

  $("#illustrations .illustration a").fancybox({titlePosition: "over"});
  initLinks($("#content"));

// социальные сервисы
  var url;
  try
  {
    url = $fs.getFrameDoc($("#ajax-content")[0]).location.pathname;
  }
  catch (e)
  {
    url = "";
  }
  if (!url)
    url = document.location.pathname;
  url = [$.baseUrl, url].join("");
  var $imgCont  = $("#illustrations");
  var $imgLinks = $imgCont.find(".illustration a");
  var img       = $imgLinks.length ? $imgLinks[$imgCont.data("main-img") || 0].href : "";
  $("#content .service a").each(function()
  {
    var $this   = $(this).attr({href: this.href.replace("%t%", document.title).replace("%u%", url).replace("%i%", img), target: "_blank"});
    var request = $this.data("getcount");
    if (request)
    {
      var $div = $("<div />", {html: "<span class='l-count' /><span class='bg-count'>0</span><span class='r-count' />"}).insertAfter($this);
      $.getJSON(request.replace("%u%", url) + "=?", function(data)
      {
        var count = 0;
        try
        {
          count = eval(["data", $this.data("count")].join(""));
        }
        catch (e) {}
        $div.find(".bg-count").text(count);
      });
    }
  });
  var $vkComments = $("#vk-discussion");
  if ($vkComments.length && window.VK)
  {
    VK.Widgets.Like("vk_like", {type: "full", width: 790, pageUrl: url, pageTitle: document.title, verb: 1});
    VK.Widgets.Comments("vk_comments", {
      limit      : 10,
      attach     : "*",
      autoPublish: 0,
      width      : 800,
      pageUrl    : url,
      onChange   : function(count, comment, date)
      {
        $.post([$.baseUrl, "/comments/setlast"].join(""), {
          "pid"    : $vkComments.data("post-id"),
          "count"  : count,
          "comment": comment,
          "date"   : date
        });
      }
    });
  }
}
